using UnityEngine;
using Prota;
using UnityEngine.Rendering.Universal;
using UnityEngine.Rendering;
using Prota.Unity;

namespace Prota.VisualEffect
{

    public class ProtaPostEffectDepthSupportFeature : ScriptableRendererFeature
    {
        ProtaPostEffectDepthSupportPass pass;
        
        public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
        {
            if(renderingData.cameraData.cameraType != CameraType.Game) return;
            renderer.EnqueuePass(pass = new ProtaPostEffectDepthSupportPass());
        }

        public override void SetupRenderPasses(ScriptableRenderer renderer, in RenderingData renderingData)
        {
            base.SetupRenderPasses(renderer, renderingData);
            if(renderingData.cameraData.cameraType != CameraType.Game) return;
            pass.depthRT = renderer.cameraDepthTargetHandle;
        }

        public override void Create()
        {
        
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }




    [ExecuteAlways]
    public class ProtaPostEffectDepthSupportPass : ScriptableRenderPass
    {
        internal RTHandle depthRT;
        public ProtaPostEffectDepthSupportPass()
        {
            this.renderPassEvent = RenderPassEvent.BeforeRenderingPostProcessing;
        }
        
        public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
        {
            base.OnCameraSetup(cmd, ref renderingData);
        }

        public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
        {
            base.Configure(cmd, cameraTextureDescriptor);
        }

        public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
        {
            using(context.WithCommandBuffer(out var cmd))
            {
                cmd.name = "ProtaPostEffectDepthSupportPass";
                cmd.SetGlobalTexture("_CameraDepthTexture", depthRT);
            }
        }
        
        public override void FrameCleanup(CommandBuffer cmd)
        {
            base.FrameCleanup(cmd);
        }
    }
    
}


